package com.example.wxordersystem.entity;

import com.baomidou.mybatisplus.annotation.TableId;
import com.example.wxordersystem.enums.ProductStatusEnum;
import com.example.wxordersystem.utils.EnumUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 商品
 * @author 常红伟
 */
@Data
public class ProductInfo implements Serializable {

    @TableId
    private String productId;
    /** 名字*/
    private String productName;
    /**单价*/
    private BigDecimal productPrice;
    /**库存*/
    private Integer productStock;
    /**描述*/
    private String productDescription;
     /**小图*/
    private String productIcon;
     /**商品状态,0正常1下架*/
    private Integer productStatus = ProductStatusEnum.UP.getCode();
     /**类目编号*/
    private Integer categoryType;
     /**创建时间*/
    private Date createTime;
     /**修改时间*/
    private Date updateTime;

    @JsonIgnore
    public ProductStatusEnum getProductStatusEnum() {
        return EnumUtil.getByCode(productStatus, ProductStatusEnum.class);
    }

    /**
     * 图片链接加host拼接成完整 url
     * @param host
     * @return
     */
    public ProductInfo addImageHost(String host) {
        if (productIcon.startsWith("//") || productIcon.startsWith("http")) {
            return this;
        }

        if (!host.startsWith("http")) {
            host = "//" + host;
        }
        if (!host.endsWith("/")) {
            host = host + "/";
        }
        productIcon = host + productIcon;
        return this;
    }

}
